package com.jk11a.shangdaast.user;

import com.jk11a.shangdaast.R;

import com.jk11a.shangdaast.dao.UserDao;


import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;

import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserInfoActivity extends Activity{

	private SharedPreferences sp=null;
	private String FILE = "saveUserNamePwd";
	private Thread getuserThread;
	 private static final int MSG_SUCCESS = 0; //获取成功
	 private static final int MSG_FAILURE = 1; //获取失败
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_info);
		sp=this.getSharedPreferences(FILE, 0);
		
		if(getuserThread==null){
			getuserThread=new Thread(runnable);
			getuserThread.start();
		}
		else {
			Toast.makeText(getApplication(), "getuserThread thread 已启动",  Toast.LENGTH_LONG).show();
		}
	}
	Runnable runnable=new Runnable() {
		// 重写run()方法，此方法在新的线程中运行
		@Override
		public void run() {
			// TODO Auto-generated method stub
			UserDao userDao=new UserDao();
			User user=userDao.getuser(sp.getInt("id", 0));
			user.setId(sp.getInt("id", 0));
			// 获取 成功，向UI线程发送MSG_SUCCESS标识和books对象
			if(user!=null){
			mHandler.obtainMessage(MSG_SUCCESS,user).sendToTarget();
			}
			else{
				// 获取 失败，向UI线程发送MSG_FAILURE标识
				 mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				 return;
			}
		}
	};
	private Handler mHandler=new Handler(){
		// 重写handleMessage()方法，此方法在UI线程运行
		@Override
		public void handleMessage(Message msg){
			switch (msg.what) {
			case MSG_SUCCESS:
				Log.i("msg obj","userinfo success");
				User user=(User)msg.obj;
				Log.i("msg obj",user.getName()+"!!");
				showinfo(user);
				break;

			case MSG_FAILURE:
				Log.i("msg obj","failed");
				Toast.makeText(getApplication(), "获取用户信息失败",  Toast.LENGTH_LONG).show();
				break;
			}
		}
	};

	public void showinfo(User user){
			TextView tv_id = new TextView(this);
			TextView tv_xuehao=new TextView(this);
			TextView tv_name = new TextView(this);
			TextView tv_class = new TextView(this);
			TextView tv_college = new TextView(this);
			TextView tv_schoolcard = new TextView(this);
			tv_id.setText("用户编号：  "+user.getId());
			tv_id.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_id.setTextColor(Color.rgb(0, 0, 0));
			tv_xuehao.setText("学号:"+user.getUserid());
			tv_xuehao.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_xuehao.setTextColor(Color.rgb(0, 0, 0));
			tv_name.setText("昵称：          "+user.getName());
			tv_name.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_name.setTextColor(Color.rgb(0, 0, 0));
			tv_class.setText("班级：          "+user.getClass_());
			tv_class.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_class.setTextColor(Color.rgb(0, 0, 0));
			tv_college.setText("学院：           "+user.getCollege());
			tv_college.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_college.setTextColor(Color.rgb(0, 0, 0));
			tv_schoolcard.setText("校园卡号：  "+user.getSchoolcard());
			tv_schoolcard.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			tv_schoolcard.setTextColor(Color.rgb(0, 0, 0));
			addlayout_top(tv_id);
			addlayout(tv_name);
			addlayout(tv_xuehao);
			addlayout(tv_class);
			addlayout(tv_college);
			addlayout(tv_schoolcard);
	}
	public void addlayout_top(TextView tv){
		RelativeLayout rLayout = new RelativeLayout(this);	
		rLayout.setBackgroundResource(R.drawable.preference_first_item);
		rLayout.setTop(14);
		rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		rLayout.setGravity(Gravity.CENTER);
		rLayout.addView(tv);   
		((LinearLayout) this.findViewById(R.id.userinfo_lilayout)).addView(rLayout);
	}
	public void addlayout(TextView tv){
		RelativeLayout rLayout = new RelativeLayout(this);	
		rLayout.setBackgroundResource(R.drawable.preference_item);
		rLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,80));
		rLayout.setGravity(Gravity.CENTER);
		rLayout.addView(tv);   
		((LinearLayout) this.findViewById(R.id.userinfo_lilayout)).addView(rLayout);
	}
}
